﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "IWidget.h"
#include "OwnerPtr.h"
#include "RuntimeId.h"

#include "QSize"
#include <vector>
#include "qobjectdefs.h"
#include "GNodeWrapper.h"
#include "ObjectBrowserWidget.h"

/// 本文件是界面实现的辅助类，不包含GDMP API的演示内容

namespace gcmp
{
    class IParameterDisplay;
    class IUserTransaction;
    class IUiDocumentEventHandler;
    class IUiViewEventHandler;
    class ISelectionChangeEventHandler;
}

namespace Sample
{
    class IFieldProxy;
    class CPropertyItem;

    /// GRep探针浏览器
    class ObjectBrowser : public CObjectBrowserWidget, public gcmp::IWidget
    {
        Q_OBJECT

    public:
        typedef std::function<void(IGraphicsNode_Wrapper* pGNodeWrapper)> OnApplyCallBack;

        template<typename T>
        static OnApplyCallBack CreateOnApplyCallBack(void (T::*func)(IGraphicsNode_Wrapper* pGNodeWrapper), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }
    public:
        ObjectBrowser();
        virtual ~ObjectBrowser();
        //获取GRep探针浏览器的唯一实例
        static ObjectBrowser* Get();
        static ObjectBrowser* GetOrCreate();
        static std::wstring GetTypeName() { return L"GRep浏览器"; };

        //显示GRep探针浏览器面板
        static void DisplayPanel();

        QSize sizeHint() const;

        virtual void SetEnabled(bool bEnable) override;
        /// 目前没有被使用，仅为了实现接口
        virtual void UpdateStatus() override;

        void SetDataAndUpdate(IGraphicsNode_Wrapper* pObjectWrapper);
        // 这个回调可以用于在成员被修改后更新到图元
        void SetOnApplyCallBack(OnApplyCallBack applyCallBack) { m_onApplyCallBack = applyCallBack; }

        IGraphicsNode_Wrapper* GetGNodeWrapper() { return m_pObjectWrapper; }
    private:

        // 应用按钮点击处理
        virtual void OnApply() override;
    protected:
        virtual void showEvent(QShowEvent *event)override;

        protected slots:
        void onPropertyEditButtonClicked(int uniquiID);
        void OnDropDownListIndexChange(int groupIndex, int propertyIndex);

    private:
        void UpdatePropertyTable(bool bRebuild = false);
        void SetPropertyItemValue(CPropertyItem* pItem, IFieldProxy* param);
        // 编辑修改属性触发的事件
        virtual void OnPropertyValueChanged(int groupIndex, int propertyIndex) override;
        //辅助函数
    private:
        bool IsDockTopWidget();

    private:
        bool    m_isTopVisible; // 标示是否在层叠浮动窗口的最上层
        bool    m_isNeedUpdate; // 是否需要更新(用于连续修改延时更新）
        gcmp::OwnerPtr<gcmp::IUserTransaction>                m_opUserTransaction;
        bool    m_isEnabled;

        gcmp::RuntimeId m_curUiDocId;
        bool m_bTransactionIsCommitting;
        bool m_canPropertyTableBeUpdated;

        // GRep探针树的根节点，即GRep的wrapper
        IGraphicsNode_Wrapper* m_pObjectWrapper;

        OnApplyCallBack m_onApplyCallBack;
    };
}

